local dao = require "dao.globaldbdao"
local filelog = require "filelog"
local tonumber = tonumber
local timetool = require "timetool"
local tabletool = require "tabletool"
local configdao = require "configdao"
local json = require "cjson"
local base = require "base"

json.encode_sparse_array(true,1,1)

--[[
	主要用于记录全局数据

	key作为每条记录的关键索引，其他需要使用者自己设计
]]
--[[
	request/notice {
		key,	   全局数据key
		rediscmd,  redis操作命令
		rediskey,  redis数据key
		--都是可选的使用时一定是按顺序添加
		rediscmdopt1, redis命令选项1
		rediscmdopt2, redis命令选项2
		rediscmdopt3,
		rediscmdopt4,
		rediscmdopt5,

		mysqltable,  mysql数据表名
		mysqldata, mysqldata表示存入mysql的数据, 一定是table
		mysqlcondition, 是一个表格或是字符串（字符串表示完整sql语句）

		choosedb, 1 表示redis， 2表示mysql，3表示redis+mysql
	}

	response {
		issuccess, 表示成功或失败
		isredisormysql, true表示返回的是mysql数据 false表示是redis数据（业务层可能要进行不同的处理）
		data, 返回的数据
	}
]]

local GlobalDataDao = {}

function GlobalDataDao.query_tablerecord(id, gameid)
	local responsemsg
	local tablerecords = {}
	local record
	if id == nil or gameid == nil then
		filelog.sys_error("GlobalDataDao.query_tablerecord invalid id or gameid")
		return nil, nil
	end
	local requestmsg = {
		key = id,
		rediscmd = "lrange",
		rediskey = "tablerecord:"..id..":"..gameid,
		rediscmdopt1 = 0,
		rediscmdopt2 = -1,
		mysqltable = "global_tablerecord",
		mysqlcondition = {
			id = id,
			gameid = gameid,
		},
		choosedb = 3,
	}

	responsemsg = dao.query(id, requestmsg)
	if responsemsg == nil then
		filelog.sys_error("GlobalDataDao.query_tablerecords failed because cannot access globaldbsvrd")
		return nil, nil		
	end

	if not responsemsg.issuccess then
		filelog.sys_error("GlobalDataDao.query_tablerecords failed because globaldbsvrd exception")		
		return nil, nil
	end

	if (responsemsg.data == nil or tabletool.is_emptytable(responsemsg.data)) 
		and responsemsg.isredisormysql then
		return true, tablerecords
	elseif responsemsg.isredisormysql then
		for _, value in ipairs(responsemsg.data) do
			record = json.decode(value.record)
			table.insert(tablerecords, record)
			GlobalDataDao.save_tablerecord("insert", id, gameid, record, 1) 
		end
		return true, tablerecords
	end

	for _, value in ipairs(responsemsg.data) do
		if value ~= nil and string.sub(value, 1,1) == "{" then
			table.insert(tablerecords, json.decode(value))
		end
	end

	return false, tablerecords
end

function GlobalDataDao.save_tablerecord(cmd, id, gameid, record, choosedb)
	if cmd == nil then
		filelog.sys_error("GlobalDataDao.save_tablerecord invalid params")
		return
	end
	local noticemsg = {
		key = id,
		mysqltable = "global_tablerecord",
		mysqlcondition = {
			id = id,
			gameid = gameid,
		},
		choosedb = 3,
	}

	if choosedb ~= nil and choosedb ~= 0 then
		noticemsg.choosedb = choosedb
	end

	if cmd == "delete" then
		noticemsg.rediscmd = "del"
		noticemsg.rediskey = "tablerecord:"..id..":"..gameid
	else
		local jsonstring
		if record ~= nil then
			jsonstring = json.encode(record)
		end
		noticemsg.rediscmd = "lpush"
		noticemsg.rediskey = "tablerecord:"..id..":"..gameid
		noticemsg.rediscmdopt1 = jsonstring
		if choosedb ~= 1 then
			noticemsg.mysqldata = {
				id = id,
				gameid = gameid,
				record = jsonstring,
			}
		end
	end

	local f = dao[cmd]
	if f == nil then
		filelog.sys_error("GlobalDataDao.save_tablerecord invalid cmd", cmd, record)
		return
	end
	f(id, noticemsg)
end

function GlobalDataDao.query_system_messages()
	local responsemsg
	local messages = {}
	local requestmsg = {
		key = "system_message",
		rediscmd = "get",
		rediskey = "sysmessages",
		mysqltable = "global_system_message",
		mysqlcondition = {},
		choosedb = 3,
	}

	responsemsg = dao.query(requestmsg.key, requestmsg)
	if responsemsg == nil then
		filelog.sys_error("GlobalDataDao.query_system_messages failed because cannot access globaldbsvrd")
		return nil, nil		
	end

	if not responsemsg.issuccess then
		filelog.sys_error("GlobalDataDao.query_system_messages failed because globaldbsvrd exception")		
		return nil, nil
	end

	if (responsemsg.data == nil or tabletool.is_emptytable(responsemsg.data)) 
		and responsemsg.isredisormysql then
		return true, messages
	elseif responsemsg.isredisormysql then
		for _, value in ipairs(responsemsg.data) do
			value.update_time = nil
			GlobalDataDao.save_system_message("insert", nil, value, 1)
		end
		return true, responsemsg.data
	end
	table.insert(messages, json.decode(responsemsg.data))
	messages[1].update_time = nil
	return false, messages
end

function GlobalDataDao.save_system_message(cmd, id, system_message, choosedb)
	if cmd == nil then
		filelog.sys_error("GlobalDataDao.save_system_message invalid params")
		return
	end

	local noticemsg = {
		key = "system_message",
		rediscmd = "set",
		rediskey = "sysmessages",

		mysqltable = "global_system_message",
		choosedb = 3,
	}

	if choosedb ~= nil then
		noticemsg.choosedb = choosedb
	end
	
	if cmd == "delete" then
		noticemsg.rediscmd = "del"
		noticemsg.rediskey = "sysmessages"
	else
		local jsonstring
		if system_message ~= nil then
			jsonstring = json.encode(system_message)
		end
		noticemsg.rediscmdopt1 = jsonstring
		if choosedb ~= 1 then
			noticemsg.mysqldata = {
				content = system_message.content,
			}
			noticemsg.mysqlcondition = {
				id = id,
			}
		end
	end

	local f = dao[cmd]
	if f == nil then
		filelog.sys_error("GlobalDataDao.save_system_message invalid cmd", cmd, system_message)
		return
	end
	f(noticemsg.key, noticemsg)
end

function GlobalDataDao.query_recharge_tips()
	local responsemsg
	local tips = {}
	local requestmsg = {
		key = "recharge_tips",
		rediscmd = "get",
		rediskey = "rechargetips",
		mysqltable = "global_recharge_tips",
		mysqlcondition = {},
		choosedb = 3,
	}

	responsemsg = dao.query(requestmsg.key, requestmsg)
	if responsemsg == nil then
		filelog.sys_error("GlobalDataDao.query_recharge_tips failed because cannot access globaldbsvrd")
		return nil, nil		
	end

	if not responsemsg.issuccess then
		filelog.sys_error("GlobalDataDao.query_recharge_tips failed because globaldbsvrd exception")		
		return nil, nil
	end

	if (responsemsg.data == nil or tabletool.is_emptytable(responsemsg.data)) 
		and responsemsg.isredisormysql then
		return true, tips
	elseif responsemsg.isredisormysql then
		for _, value in ipairs(responsemsg.data) do
			value.update_time = nil
			GlobalDataDao.save_system_message("insert", nil, value, 1)
		end
		return true, responsemsg.data
	end

	table.insert(tips, json.decode(responsemsg.data))
	tips[1].update_time = nil
	return false, tips
end

function GlobalDataDao.save_recharge_tip(cmd, id, recharge_tip, choosedb)
	if cmd == nil then
		filelog.sys_error("GlobalDataDao.save_recharge_tip invalid params")
		return
	end

	local noticemsg = {
		key = "recharge_tips",
		rediscmd = "set",
		rediskey = "rechargetips",
		mysqltable = "global_recharge_tips",
		choosedb = 3,
	}

	if choosedb ~= nil then
		noticemsg.choosedb = choosedb
	end
	
	if cmd == "delete" then
		noticemsg.rediscmd = "del"
		noticemsg.rediskey = "rechargetips"
	else
		local jsonstring
		if recharge_tip ~= nil then
			jsonstring = json.encode(recharge_tip)
		end
		noticemsg.rediscmdopt1 = jsonstring
		if choosedb ~= 1 then
			noticemsg.mysqldata = {
				content = recharge_tip.content,
			}
			noticemsg.mysqlcondition = {
				id = id,
			}
		end
	end

	local f = dao[cmd]
	if f == nil then
		filelog.sys_error("GlobalDataDao.save_recharge_tip invalid cmd", cmd, recharge_tip)
		return
	end
	f(noticemsg.key, noticemsg)

	if cmd == nil then
		filelog.sys_error("GlobalDataDao.save_recharge_tip invalid params")
		return
	end
end

function GlobalDataDao.query_marquees()
	local responsemsg
	local marquees = {}
	local requestmsg = {
		key = "marquees",
		rediscmd = "get",
		rediskey = "marquees",
		mysqltable = "global_marquee",
		mysqlcondition = {},
		choosedb = 3,
	}

	responsemsg = dao.query(requestmsg.key, requestmsg)
	if responsemsg == nil then
		filelog.sys_error("GlobalDataDao.query_marquee failed because cannot access globaldbsvrd")
		return nil, nil		
	end

	if not responsemsg.issuccess then
		filelog.sys_error("GlobalDataDao.query_marquee failed because globaldbsvrd exception")		
		return nil, nil
	end

	if (responsemsg.data == nil or tabletool.is_emptytable(responsemsg.data)) 
		and responsemsg.isredisormysql then
		return true, marquees
	elseif responsemsg.isredisormysql then
		for _, value in ipairs(responsemsg.data) do
			value.update_time = nil
			GlobalDataDao.save_marquee("insert", nil, value, 1)
		end
		return true, responsemsg.data
	end

	table.insert(marquees, json.decode(responsemsg.data))
	marquees[1].update_time = nil
	return false, marquees
end

function GlobalDataDao.save_marquee(cmd, id, marquee, choosedb)
	if cmd == nil then
		filelog.sys_error("GlobalDataDao.save_marquee invalid params")
		return
	end

	local noticemsg = {
		key = "marquees",
		rediscmd = "set",
		rediskey = "marquees",
		mysqltable = "global_marquee",
		choosedb = 3,
	}

	if choosedb ~= nil then
		noticemsg.choosedb = choosedb
	end
	
	if cmd == "delete" then
		noticemsg.rediscmd = "del"
		noticemsg.rediskey = "marquees"
	else
		local jsonstring
		if marquee ~= nil then
			jsonstring = json.encode(marquee)
		end
		noticemsg.rediscmdopt1 = jsonstring
		if choosedb ~= 1 then
			noticemsg.mysqldata = {
				content = marquee.content,
			}
			noticemsg.mysqlcondition = {
				id = id,
			}
		end
	end

	local f = dao[cmd]
	if f == nil then
		filelog.sys_error("GlobalDataDao.save_marquee invalid cmd", cmd, marquee)
		return
	end
	f(noticemsg.key, noticemsg)

	if cmd == nil then
		filelog.sys_error("GlobalDataDao.save_marquee invalid params")
		return
	end
end

function GlobalDataDao.query_shares()
	local responsemsg
	local shares = {}
	local requestmsg = {
		key = "shares",
		rediscmd = "get",
		rediskey = "shares",
		mysqltable = "global_share",
		mysqlcondition = {},
		choosedb = 3,
	}

	responsemsg = dao.query(requestmsg.key, requestmsg)
	if responsemsg == nil then
		filelog.sys_error("GlobalDataDao.query_shares failed because cannot access globaldbsvrd")
		return nil, nil		
	end

	if not responsemsg.issuccess then
		filelog.sys_error("GlobalDataDao.query_shares failed because globaldbsvrd exception")		
		return nil, nil
	end

	if (responsemsg.data == nil or tabletool.is_emptytable(responsemsg.data)) 
		and responsemsg.isredisormysql then
		return true, shares
	elseif responsemsg.isredisormysql then
		for _, value in ipairs(responsemsg.data) do
			value.update_time = nil
			GlobalDataDao.save_share("insert", nil, value, 1)
		end
		return true, responsemsg.data
	end

	table.insert(shares, json.decode(responsemsg.data))
	return false, shares
end

function GlobalDataDao.save_share(cmd, id, share, choosedb)
	if cmd == nil then
		filelog.sys_error("GlobalDataDao.save_share invalid params")
		return
	end

	local noticemsg = {
		key = "shares",
		rediscmd = "set",
		rediskey = "shares",
		mysqltable = "global_share",
		choosedb = 3,
	}

	if choosedb ~= nil then
		noticemsg.choosedb = choosedb
	end
	
	if cmd == "delete" then
		noticemsg.rediscmd = "del"
		noticemsg.rediskey = "shares"
	else
		local jsonstring
		if share ~= nil then
			jsonstring = json.encode(share)
		end
		noticemsg.rediscmdopt1 = jsonstring
		if choosedb ~= 1 then
			noticemsg.mysqldata = {
				content = share.content,
			}
			noticemsg.mysqlcondition = {
				id = id,
			}
		end
	end

	local f = dao[cmd]
	if f == nil then
		filelog.sys_error("GlobalDataDao.save_share invalid cmd", cmd, share)
		return
	end
	f(noticemsg.key, noticemsg)

	if cmd == nil then
		filelog.sys_error("GlobalDataDao.save_share invalid params")
		return
	end
end

return GlobalDataDao
